package com.dingxin.ship_together_api.schedule;

import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.session.SaSessionCustomUtil;
import com.dingxin.ship_together_api.entity.SysRole;
import com.dingxin.ship_together_api.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Author: ctj
 * Date: 2022-09-09
 * Time: 15:25
 * 清除全局Session
 */
@Component
public class CleanCustomSessionSchedule {

    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 每隔30秒清除全局Session
     */
    @Scheduled(cron = "0/30 * * * * ? ")
    private void cleanCustomSession(){
        List<SysRole> sysRoles = sysUserMapper.selectAllRole();
        sysRoles.forEach(role->{
            SaSession session = SaSessionCustomUtil.getSessionById("role-" + role.getCode());
            session.clear();
        });
    }

}
